package marchsoft.modules.four.h5.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import marchsoft.annotation.AnonymousAccess;
import marchsoft.base.PageVO;
import marchsoft.modules.four.common.entity.SafeCase;
import marchsoft.modules.four.common.entity.SafeType;
import marchsoft.modules.four.common.entity.Slideshow;
import marchsoft.modules.four.common.entity.StudyData;
import marchsoft.modules.four.h5.service.Impl.HomeServiceImpl;
import marchsoft.response.Result;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页控制
 * 提供
 *  轮播图
 *  轮播图跳转
 *  分类
 *  今日推荐
 *  经典案例
 *  导航跳转
 * 控制
 */
@RestController//声明bean
@RequestMapping("/api/h5")//控制访问指向
@Api(tags = "首页控制_akuner")//标注
public class HomeController {
    //注入 service
    private final HomeServiceImpl homeService;

    public HomeController(HomeServiceImpl homeService) {
        this.homeService = homeService;
    }

    /**
     * 轮播图控制接口
     * @return Result<List<Slideshow>> : 轮播图详细信息
     */
    @GetMapping("/eSlideshow/all")
    @AnonymousAccess
    @ApiOperation(value = "轮播图测试接口",notes = "author:akuner")
    public Result<List<Slideshow>> eSlideshowAll (){
        return Result.success(homeService.getESlideshowAll());
    }

    /**
     * 轮播图跳转控制
     * @return Result<List<StudyData>> ： 每个轮播图对应的文章详情
     */
    @RequestMapping("/eStudyData/front")
    @ApiOperation(value = "轮播图跳转测试接口",notes = "author:akuner")
    public Result<List<StudyData>> eStudyDataFront (@RequestParam String id){
        return Result.success(homeService.getEStudyDataFront(Long.parseLong(id)));
    }

    /**
     * 轮播图 内部 点赞控制
     * @return Result<Object> ： 点赞结果
     */
    @RequestMapping("/eStudyData/front/like")
    @ApiOperation(value = "点赞测试接口",notes = "author:akuner")
    public Result<Object> eStudyDataFrontLike (@RequestBody String id){
        JSONObject parse = (JSONObject) JSONObject.parse(id);
        if((boolean) parse.get("isTags")){
            return Result.success(homeService.getEStudyDataFrontLike(Long.parseLong(parse.get("id") + "")) ? "点赞成功" : "点赞失败");
        }
        return Result.success(homeService.getEStudyDataFrontUnLike(Long.parseLong(parse.get("id") + "")) ? "取消点赞成功" : "取消点赞失败");
    }

    /**
     * 轮播图 内部 踩一踩 控制
     * @return Result<Object> ： 踩的结果
     */
    @RequestMapping("/eStudyData/front/dislike")
    @ApiOperation(value = "点赞测试接口",notes = "author:akuner")
    public Result<Object> eStudyDataFrontDislike (@RequestBody String id){
        JSONObject parse = (JSONObject) JSONObject.parse(id);
        if((boolean) parse.get("isDislike")){
            return Result.success(homeService.getEStudyDataFrontDislike(Long.parseLong(parse.get("id") + "")) ? "踩成功" : "踩失败");
        }
        return Result.success(homeService.getEStudyDataFrontUnDislike(Long.parseLong(parse.get("id") + "")) ? "取消踩成功" : "取消踩失败");
    }

    /**
     * 看了看
     */
    @RequestMapping("/eStudyData/front/read")
    @ApiOperation(value = "点赞测试接口",notes = "author:akuner")
    public Result<Object> eStudyDataFrontRead (@RequestBody String id){
        JSONObject parse = (JSONObject) JSONObject.parse(id);
        return Result.success(homeService.getEStudyDataFrontRead(Long.parseLong(parse.get("id") + ""), Integer.parseInt(parse.get("count") + "")));
    }

    /**
     * 分类图标 控制
     * @return Result<List<SafeType>> : 分类详情列表
     */
    @RequestMapping("/eSafeType/frontAll")
    @AnonymousAccess
    @ApiOperation(value = "点赞测试接口",notes = "author:akuner")
    public Result<List<SafeType>> eSafeTypeFrontAll (){
        return Result.success(homeService.getESafeTypeFrontAll());
    }

    /**
     * 获取所有安全分类名称
     * @return Result<List<SafeType>> : 案例推荐列表
     */
    @RequestMapping("/eSafeType/allName")
    @AnonymousAccess
    @ApiOperation(value = "案例推荐测试接口",notes = "author:akuner")
    public Result<List<SafeType>> eStudyDataFrontAllParams (){
        return Result.success(homeService.getESafeTypeFrontAll());
    }

    /**
     * 今日推荐
     * @return Result<List<SafeType>> : 今日推荐列表
     */
    @RequestMapping("/eStudyData/front/daily")
    @AnonymousAccess
    @ApiOperation(value = "今日推荐案例测试接口",notes = "author:akuner")
    public Result<List<StudyData>> eStudyDataFrontDaily(@RequestParam int current, @RequestParam int size){
        return Result.success(homeService.getEStudyDataFrontDaily((current - 1)*size));
    }

    /**
     * 经典案例推荐
     * @return Result<List<SafeType>> : 经典案例推荐列表
     */
    @RequestMapping("/eSafeCase/front-all")
    @AnonymousAccess
    @ApiOperation(value = "经典案例推荐测试接口",notes = "author:akuner")
    public Result<List<SafeCase>> eSafeCaseFrontAll(@RequestParam int current, @RequestParam int size){
        return Result.success(homeService.getESafeCaseFrontAll((current - 1)*size));
    }

}
